package com.tegistuff.webos.run.debugger.breakpoints;

import com.intellij.xdebugger.breakpoints.XBreakpointProperties;
import com.intellij.xdebugger.breakpoints.XLineBreakpoint;

import java.util.*;

/**
 * WebOSBreakpointMap.java
 *
 * This class maps IDEA breakpoints onto filename & line pairs and vice versa.
 *
 * Copyright (C) 2011-2012 Timo Tegtmeier.
 * All rights reserved.
 */
public class WebOSBreakpointMap {
    
    private Dictionary<String, Dictionary<Integer, WebOSBreakpointWrapper>> myLocation2wrapperMap;
    private Dictionary<XLineBreakpoint<XBreakpointProperties>, WebOSBreakpointWrapper> myBreakpoint2wrapperMap;

    public WebOSBreakpointMap() {
        myLocation2wrapperMap = new Hashtable<String, Dictionary<Integer, WebOSBreakpointWrapper>>();
        myBreakpoint2wrapperMap = new Hashtable<XLineBreakpoint<XBreakpointProperties>, WebOSBreakpointWrapper>();
    }
    
    public void add(WebOSBreakpointWrapper breakpointWrapper) {
        Dictionary<Integer, WebOSBreakpointWrapper> dict = getBreakpoints(breakpointWrapper.getFileName());
        if(dict == null) {
            dict = new Hashtable<Integer, WebOSBreakpointWrapper>();
            myLocation2wrapperMap.put(breakpointWrapper.getFileName(), dict);
        }
        dict.put(breakpointWrapper.getLineNumber(), breakpointWrapper);
        myBreakpoint2wrapperMap.put(breakpointWrapper.getBreakpoint(), breakpointWrapper);
    }
    
    public void remove(WebOSBreakpointWrapper breakpointWrapper) {
        remove(breakpointWrapper.getFileName(), breakpointWrapper.getLineNumber());
    }
    
    public void remove(String fileName, int lineNumber) {
        Dictionary<Integer, WebOSBreakpointWrapper> dict = getBreakpoints(fileName);
        if(dict == null)
            return;
        WebOSBreakpointWrapper wrapper = dict.get(lineNumber);
        dict.remove(lineNumber);
        if(dict.isEmpty())
            myLocation2wrapperMap.remove(fileName);
        myBreakpoint2wrapperMap.remove(wrapper.getBreakpoint());
    }
       
    public WebOSBreakpointWrapper find(String fileName, int lineNumber) {
        Dictionary<Integer, WebOSBreakpointWrapper> dict = getBreakpoints(fileName);
        if(dict == null)
            return null;
        return dict.get(lineNumber);
    }

    public WebOSBreakpointWrapper find(XLineBreakpoint<XBreakpointProperties> breakpoint) {
        return myBreakpoint2wrapperMap.get(breakpoint);
    }

    private Dictionary<Integer, WebOSBreakpointWrapper> getBreakpoints(String fileName) {
        return myLocation2wrapperMap.get(fileName);
    }

    public List<WebOSBreakpointWrapper> getAllBreakpoints() {
        List<WebOSBreakpointWrapper> result = new LinkedList<WebOSBreakpointWrapper>();
        Enumeration<String> files = myLocation2wrapperMap.keys();
        while(files.hasMoreElements()) {
            String file = files.nextElement();
            Dictionary<Integer, WebOSBreakpointWrapper> fileBreakpoints = myLocation2wrapperMap.get(file);
            Enumeration<Integer> lines = fileBreakpoints.keys();
            while (lines.hasMoreElements()) {
                result.add(fileBreakpoints.get(lines.nextElement()));
            }
        }
        return result;
    }
}
